package com.san.hkhospital.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.san.hkhospital.entity.Medicine;
import com.san.hkhospital.service.MedBuyService;
import com.san.hkhospital.service.MedicineService;
import com.san.hkhospital.service.impl.MedicineServiceImpl;
import com.san.hkhospital.dto.Medicine1;
import com.san.hkhospital.vo.MedicineVo;
import com.san.hkhospital.vo.ResultVO;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* (medicine)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/medicine")
@CrossOrigin
public class MedicineController {
    /**
     * 服务对象
     */
    @Resource
    private MedicineServiceImpl medicineServiceImpl;
    @Resource
    private MedicineService medicineService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Medicine selectOne(Integer id) {
        return medicineServiceImpl.selectByPrimaryKey(id);
    }

    //展示页面
    @RequestMapping("medicineList")
    public ResultVO medicineList(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Medicine> medicineList = medicineService.medicineList();
        PageInfo<Medicine> pageInfo = new PageInfo<Medicine>(medicineList);
        return ResultVO.success("查询完成", pageInfo);
    }


    @RequestMapping("search")
    public ResultVO Search(@RequestBody Medicine1 medicine1) {
        System.out.println(medicine1);
        PageHelper.startPage(medicine1.getPageNum(),medicine1.getPageSize());
        List<MedicineVo> medicineList = medicineService.search(medicine1);
        PageInfo<MedicineVo> pageInfo = new PageInfo<>(medicineList);
        return ResultVO.success("查询完成", pageInfo);
    }

    //添加药品
    @PostMapping
    public ResultVO addMedicine(@RequestBody Medicine medicine) {
        boolean flag = medicineService.addMedicine(medicine);
        if (flag) {
            return ResultVO.success("药品添加成功");
        }else{
            return ResultVO.failed("药品添加失败");
        }

    }

    //删除药品
    @DeleteMapping(value = "/{id}")
    public ResultVO deleteMedicine(@PathVariable Integer id) {
        boolean flag = medicineService.deleteMedicine(id);
        if (flag) {
           return ResultVO.success("药品删除成功");
        }else {
            return ResultVO.failed("药品删除失败");
        }
    }

    //药品价格修改
    @RequestMapping("updateMedicine")
    public ResultVO updateMedicine(@RequestBody Medicine medicine){
        int i = medicineService.updateMedicine(medicine);
        if(i != 0){
            return  ResultVO.success("药单价格已更改");
        }
            return ResultVO.failed("药单价格未更改");
    }



}

